package keye.document_manage.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import java.awt.FlowLayout;

import javax.swing.border.TitledBorder;

import java.awt.Color;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import keye.document_manage.util.MyConstants;

public class ConnectView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JPasswordField textField_3;
	private JButton connectBtn;
	private JCheckBox checkBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConnectView frame = new ConnectView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConnectView() {
		init();
	}
	public void init(){
		
		initComponents();
		initEvents();
		try {
			Path p = Paths.get("./connectionInfo.txt");
			if(Files.notExists(p)){
				Files.createFile(p);
			}
			Files.readAllLines(p).forEach(line->{
				if(line.startsWith("sqlserver.ip=")){
					textField.setText(line.replace("sqlserver.ip=",""));
				}else if(line.startsWith("sqlserver.dbname=")){
					textField_1.setText(line.replace("sqlserver.dbname=",""));
				}else if(line.startsWith("sqlserver.username=")){
					textField_2.setText(line.replace("sqlserver.username=",""));
				}else if(line.startsWith("sqlserver.password=")){
					textField_3.setText(line.replace("sqlserver.password=",""));
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void initEvents() {
		connectBtn.addActionListener(e->{
			String url = textField.getText().trim();
			String dbName = textField_1.getText().trim();
			String userName = textField_2.getText().trim();
			String userPass = String.valueOf(textField_3.getPassword()).trim();
			try {
				String tmpPass = checkBox.isSelected()?userPass:"";
				Files.write(Paths.get("./connectionInfo.txt"), ("sqlserver.ip="+url+System.getProperty("line.separator")
						+"sqlserver.dbname="+dbName+System.getProperty("line.separator")
						+"sqlserver.username="+userName+System.getProperty("line.separator")
						+"sqlserver.password="+tmpPass).getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
				StringBuilder sb = new StringBuilder();
				Path p = Paths.get(MyConstants.PROPERTIES_PATH+"jdbc.properties");
				Files.readAllLines(p).forEach(line->{
					if(line.startsWith("sqlserver.url=")){
						sb.append("sqlserver.url=jdbc:jtds:sqlserver://"+url+"/"+dbName+System.getProperty("line.separator"));
					}else if(line.startsWith("sqlserver.username=")){
						sb.append("sqlserver.username="+userName+System.getProperty("line.separator"));
					}else if(line.startsWith("sqlserver.password=") ){
						sb.append("sqlserver.password="+userPass+System.getProperty("line.separator"));
					}else {
						sb.append(line+System.getProperty("line.separator"));
					}
				});
				Files.write(p, sb.toString().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		});
	}

	public void initComponents(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setBounds(100, 100, 450, 300);
		setResizable(false);
		setAlwaysOnTop(true);
		setLocationRelativeTo(null);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(10, 10));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		Border border = BorderFactory.createLineBorder(Color.GRAY, 2); 
		panel.setBorder(new TitledBorder(border, "\u6570\u636E\u5E93\u8FDE\u63A5", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
	
		contentPane.add(panel, BorderLayout.CENTER);
		
		JLabel lblip = new JLabel("主机名或IP地址：");
		
		JLabel label = new JLabel("数据库名：");
		
		JLabel label_1 = new JLabel("用户名：");
		
		JLabel label_2 = new JLabel("密码：");
		
		checkBox = new JCheckBox("保存密码");
		checkBox.setSelected(true);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		textField_3 = new JPasswordField();
		textField_3.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(22)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(label_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblip, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(textField_1, Alignment.LEADING)
							.addComponent(textField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
							.addComponent(textField_3)
							.addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
							.addComponent(checkBox, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)))
					.addGap(14))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblip, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(checkBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		contentPane.add(panel_1, BorderLayout.SOUTH);
		
		connectBtn = new JButton("连接");
		panel_1.add(connectBtn);
		
		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.WEST);
		
		JPanel panel_3 = new JPanel();
		contentPane.add(panel_3, BorderLayout.EAST);
		
	}
	
	public JButton getConnectBtn() {
		return connectBtn;
	}
	
}
